<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            思路：二分。计算出数组的mid值，左边一定小于mid，所以
            可以直接作为左子树，右边一定大于它，作为右子树
            */
            var sortedArrayToBST = function (nums) {
                function buildTree(arr, left, right) {
                    //二分的终止条件
                    if (left > right) {
                        return null
                    }
                    let mid = Math.floor((left + right) / 2)
                    let root = new TreeNode(arr[mid])
                    root.left = buildTree(arr, left, mid - 1)
                    root.right = buildTree(arr, mid + 1, right)
                    return root
                }
                buildTree(nums, 0, nums.length - 1)
            }
        </script>
    </body>
</html>
